package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.jsonresult.JsonResult;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     * @param dish
     * @return
     */
    @Transactional
    @CacheEvict(value = "dish", allEntries = true)
    @PostMapping
    public JsonResult add(@RequestBody Dish dish){
        return dishService.add(dish);
    }

    /**
     *修改菜品
     * @param dish
     * @return
     */
    @Transactional
    @CacheEvict(value = "dish", allEntries = true)
    @PutMapping
    public JsonResult put(@RequestBody Dish dish){
        return dishService.put(dish);
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @CacheEvict(value = "dish", allEntries = true)
    @DeleteMapping
    public JsonResult delete(String ids) {
        return dishService.delete(ids);
    }

    /**
     * 菜品停售/起售
     * @param status
     * @param ids
     * @return
     */
    @CacheEvict(value = "dish", allEntries = true)
    @PostMapping("/status/{status}")
    public JsonResult status(@PathVariable String status, String ids) {
        return dishService.status(status,ids);
    }

    /**
     * 菜品下拉列表
     * @return
     */
    @GetMapping("/list")
    public JsonResult dishList(String categoryId){
        return dishService.dishList(categoryId);
    }

    /**
     * 菜品信息分页查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public JsonResult getPage(String name, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize){
        return dishService.getPage(name, page, pageSize);
    }

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public JsonResult getDishById(@PathVariable String id) {
        return dishService.getDishById(id);
    }





}
